//
//  UIViewController+Tool.m
//  PaasLocal
//
//  Created by 余意 on 2018/7/2.
//  Copyright © 2018年 余意. All rights reserved.
//

#import "UIViewController+Tool.h"

@implementation UIViewController (Tool)

- (UIView *)currentView
{
    return [self currentViewController].view;
}

- (UIViewController *)currentViewController
{
    UIViewController * rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
    return [self currentViewControllerFromRootViewController:rootViewController];
}

- (UIViewController *)currentViewControllerFromRootViewController:(UIViewController *)rootVC
{
    UIViewController * currentVC;
    if ([rootVC presentedViewController])
    {
        //vc是被presented出来的
        rootVC = [rootVC presentedViewController];
    }
    
    if ([rootVC isKindOfClass:[UITabBarController class]])
    {
        //跟视图是UITabbarController
        currentVC = [self currentViewControllerFromRootViewController:[(UITabBarController *)rootVC selectedViewController]];
    }
    else if ([rootVC isKindOfClass:[UINavigationController class]])
    {
        //跟视图是UINavigationController
        currentVC = [self currentViewControllerFromRootViewController:[(UINavigationController *)rootVC visibleViewController]];
    }
    else
    {
        //跟视图不是导航类
        currentVC = rootVC;
    }
    
    return currentVC;
}

- (UIView *)lastWindow
{
    return [UIApplication sharedApplication].delegate.window;
}

@end
